from google.appengine.api import app_identity
import urllib2
import urllib
import json
import datetime
import operator

apiKey = "AIzaSyCtVtAW9-O9ICRsaeGaWMqqAdrwA0h3za4"
scopes = ['https://www.googleapis.com/auth/mapsengine.readonly']
# this is the asset id obtained from maps engine
incidentsTableId = "16075412419356476951-18218961474515905548"
# this will scale the max vaxis value and the circle radius down by a factor of:
dividefactor = 12

# rawData = ((u'Chancellery Lane', u'-33.91699916', u'151.2342069', 1, 14, datetime.date(2013, 11, 15), None), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 1, 2, datetime.date(2013, 11, 15), datetime.time(1, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 0, 1, datetime.date(2013, 11, 15), datetime.time(2, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 0, 0, datetime.date(2013, 11, 15), datetime.time(3, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 1, 0, datetime.date(2013, 11, 15), datetime.time(4, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 4, 1, datetime.date(2013, 11, 15), datetime.time(5, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 25, 25, datetime.date(2013, 11, 15), datetime.time(6, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 79, 9, datetime.date(2013, 11, 15), datetime.time(7, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 337, 23, datetime.date(2013, 11, 15), datetime.time(8, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 292, 18, datetime.date(2013, 11, 15), datetime.time(9, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 174, 36, datetime.date(2013, 11, 15), datetime.time(10, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 175, 48, datetime.date(2013, 11, 15), datetime.time(11, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 205, 58, datetime.date(2013, 11, 15), datetime.time(12, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 109, 30, datetime.date(2013, 11, 15), datetime.time(13, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 45, 21, datetime.date(2013, 11, 15), datetime.time(14, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 61, 45, datetime.date(2013, 11, 15), datetime.time(15, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 41, 34, datetime.date(2013, 11, 15), datetime.time(16, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 27, 23, datetime.date(2013, 11, 15), datetime.time(17, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 22, 21, datetime.date(2013, 11, 15), datetime.time(18, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 33, 17, datetime.date(2013, 11, 15), datetime.time(19, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 16, 15, datetime.date(2013, 11, 15), datetime.time(20, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 8, 5, datetime.date(2013, 11, 15), datetime.time(21, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 4, 18, datetime.date(2013, 11, 15), datetime.time(22, 0)), (u'Chancellery Lane', u'-33.91699916', u'151.2342069', 2, 29, datetime.date(2013, 11, 15), datetime.time(23, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 0, 5, datetime.date(2013, 11, 15), None), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 1, 2, datetime.date(2013, 11, 15), datetime.time(1, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 1, 5, datetime.date(2013, 11, 15), datetime.time(2, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 6, 0, datetime.date(2013, 11, 15), datetime.time(3, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 5, 2, datetime.date(2013, 11, 15), datetime.time(4, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 8, 8, datetime.date(2013, 11, 15), datetime.time(5, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 11, 10, datetime.date(2013, 11, 15), datetime.time(6, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 77, 26, datetime.date(2013, 11, 15), datetime.time(7, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 240, 105, datetime.date(2013, 11, 15), datetime.time(8, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 220, 41, datetime.date(2013, 11, 15), datetime.time(9, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 180, 151, datetime.date(2013, 11, 15), datetime.time(10, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 178, 98, datetime.date(2013, 11, 15), datetime.time(11, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 190, 195, datetime.date(2013, 11, 15), datetime.time(12, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 190, 190, datetime.date(2013, 11, 15), datetime.time(13, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 136, 106, datetime.date(2013, 11, 15), datetime.time(14, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 118, 171, datetime.date(2013, 11, 15), datetime.time(15, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 101, 269, datetime.date(2013, 11, 15), datetime.time(16, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 80, 262, datetime.date(2013, 11, 15), datetime.time(17, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 68, 159, datetime.date(2013, 11, 15), datetime.time(18, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 47, 96, datetime.date(2013, 11, 15), datetime.time(19, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 47, 85, datetime.date(2013, 11, 15), datetime.time(20, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 29, 58, datetime.date(2013, 11, 15), datetime.time(21, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 8, 52, datetime.date(2013, 11, 15), datetime.time(22, 0)), (u'Outdoor Stairs', u'-33.91644715', u'151.2333043', 8, 58, datetime.date(2013, 11, 15), datetime.time(23, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 1, datetime.date(2013, 11, 15), None), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 0, datetime.date(2013, 11, 15), datetime.time(1, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 4, 3, datetime.date(2013, 11, 15), datetime.time(2, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 4, 2, datetime.date(2013, 11, 15), datetime.time(3, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 0, datetime.date(2013, 11, 15), datetime.time(4, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 1, datetime.date(2013, 11, 15), datetime.time(5, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 1, 1, datetime.date(2013, 11, 15), datetime.time(6, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 1, 1, datetime.date(2013, 11, 15), datetime.time(7, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 2, 1, datetime.date(2013, 11, 15), datetime.time(8, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 11, 7, datetime.date(2013, 11, 15), datetime.time(9, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 2, 4, datetime.date(2013, 11, 15), datetime.time(10, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 11, 18, datetime.date(2013, 11, 15), datetime.time(11, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 11, 11, datetime.date(2013, 11, 15), datetime.time(12, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 2, 3, datetime.date(2013, 11, 15), datetime.time(13, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 3, 4, datetime.date(2013, 11, 15), datetime.time(14, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 3, 4, datetime.date(2013, 11, 15), datetime.time(15, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 4, 6, datetime.date(2013, 11, 15), datetime.time(16, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 1, 1, datetime.date(2013, 11, 15), datetime.time(17, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 0, datetime.date(2013, 11, 15), datetime.time(18, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 0, datetime.date(2013, 11, 15), datetime.time(19, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 0, datetime.date(2013, 11, 15), datetime.time(20, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 1, datetime.date(2013, 11, 15), datetime.time(21, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 0, datetime.date(2013, 11, 15), datetime.time(22, 0)), (u'FoodCourts', u'-33.91735974', u'151.2344778', 0, 0, datetime.date(2013, 11, 15), datetime.time(23, 0)))
# classData = ((u'00:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'01:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'02:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'03:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'04:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'05:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'06:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'07:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'08:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'09:00:00', 68, datetime.date(2013, 11, 15), u'Library'), (u'10:00:00', 101, datetime.date(2013, 11, 15), u'Library'), (u'11:00:00', 109, datetime.date(2013, 11, 15), u'Library'), (u'12:00:00', 100, datetime.date(2013, 11, 15), u'Library'), (u'13:00:00', 142, datetime.date(2013, 11, 15), u'Library'), (u'14:00:00', 95, datetime.date(2013, 11, 15), u'Library'), (u'15:00:00', 86, datetime.date(2013, 11, 15), u'Library'), (u'16:00:00', 102, datetime.date(2013, 11, 15), u'Library'), (u'17:00:00', 86, datetime.date(2013, 11, 15), u'Library'), (u'18:00:00', 56, datetime.date(2013, 11, 15), u'Library'), (u'19:00:00', 36, datetime.date(2013, 11, 15), u'Library'), (u'20:00:00', 26, datetime.date(2013, 11, 15), u'Library'), (u'21:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'22:00:00', 0, datetime.date(2013, 11, 15), u'Library'), (u'23:00:00', 0, datetime.date(2013, 11, 15), u'Library'))

def getUniqueValues():
	locations = []
	dates = []
	times = []

	for row in rawData:
		locations.append(row[0])
		dates.append(row[5])
		if str(row[6]) == str(None):
			times.append("00:00:00")
		else:
			times.append("%s" % row[6])

	locations = set(locations)
	dates = set(dates)
	times = sorted(set(times))
	
	return (locations, dates, times)

def getToken():

	try:
		token, x = app_identity.get_access_token(scopes)
	except:
		token = urllib2.urlopen("http://nicksdemospace.appspot.com/unswheatmap/default/token.html").read()

	return token

	
def token():

	token, x = app_identity.get_access_token(scopes)
	
	return dict(token=token)


def getData(tableId):
	# this function gets data from a mapsengine vector table, use the below to view a sample of the data 
	# https://www.googleapis.com/mapsengine/v1/tables/16075412419356476951-18218961474515905548/features?&version=published&key=AIzaSyBOc4rumuCib6E4bymd_wWlLQm1jB4Fg80&access_token=1/NYL4SpUd6-WPm1U1jBLomTItZ8Og9FefENUNSniBsV4

	token = getToken()
	points = []

	params = {"version": "published", "key": apiKey, "access_token": token}
	encodedParams = urllib.urlencode(params)

	url = "https://www.googleapis.com/mapsengine/v1/tables/%s/features?%s" % (tableId,encodedParams)
	response = urllib2.urlopen(url)
	data = json.loads(response.read())
	
	for feature in data["features"]:
		lng = feature["geometry"]["coordinates"][0]
		lat = feature["geometry"]["coordinates"][1]
		
		points.append((lat,lng))
	
	return points


def index():

	uniqueValues = getUniqueValues()

	return dict(apiKey=apiKey, rawData=rawData, locations=uniqueValues[0], dates=uniqueValues[1], times=uniqueValues[2])


def showHeatmap():

	inputdate = request.vars.dateInput
	inputlocation = request.vars.locationInput

	if request.vars.timeInput == "00:00:00":
		inputtime = None
	else:
		inputtime = request.vars.timeInput

	pointData = []

	if inputlocation == "All":
		# the below will add all the relevant points to the pointData list then sort them
		# based on the amount of total traffic. This is so that the circles are drawn on the map
		# from largest to smallest.
		for row in rawData:
			if str(row[5]) == str(inputdate) and str(row[6]) == str(inputtime):# remove "and row[0] == inputlocation" to have all three locations show up
				pointData.append(row)
		
		pointDataSorted = []
		for point in pointData:
			point = point + (point[3]+point[4],)
			pointDataSorted.append(point)

		# commented this out so that the chart does not get sorted.
		# pointData = sorted(pointDataSorted, key=operator.itemgetter(7), reverse=True)
	else:
		for row in rawData:
			if str(row[5]) == str(inputdate) and str(row[6]) == str(inputtime) and row[0] == inputlocation:# remove "and row[0] == inputlocation" to have all three locations show up
				pointData.append(row)
	
	# This gives the max value of all the data and is used in setting the chart's minimum vaxis value.
	# invalues = []
	# outvalues = []
	# for row in rawData:
	# 	if inputlocation == "All":
	# 		invalues.append(row[3])
	# 		outvalues.append(row[4])
	# 	else:
	# 		if row[0] == inputlocation:
	# 			invalues.append(row[3])
	# 			outvalues.append(row[4])
	# 		else:
	# 			pass
	# if max(invalues) > max(outvalues):
	# 	maxValue = max(invalues)/dividefactor
	# else:
	# 	maxValue = max(outvalues)/dividefactor
	
	uniqueValues = getUniqueValues()

	classes = []
	for row in classData:
		if str(row[0]) == str(request.vars.timeInput) and str(row[2]) == str(inputdate):
			classes.append(row)
	
	return dict(pointData=pointData, locations=uniqueValues[0], dividefactor=dividefactor, classes=classes)